//
//  BaseTableViewController.m
//  learn-thread
//
//  Created by srxboys on 2021/4/22.
//

#import "BaseTableViewController.h"

@interface BaseTableViewController ()
@end

@implementation BaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.frame = self.view.bounds;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.cellBaseHeight = 100;
    self.tableView.sectionFooterHeight = 40.0f;

    [self.tableView registerClass:[ThreadTableViewCell class] forCellReuseIdentifier:ThreadCellIdentifier];

//    因为没有需要动态的数据变化，就不需要刷新
//    [self.tableView reloadData];
}

- (NSMutableArray<CellModel *> *)sourceArray {
    if (!_sourceArray) {
        _sourceArray = [[NSMutableArray alloc] init];
    }
    return _sourceArray;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    ThreadTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ThreadCellIdentifier];
    CellModel * model = self.sourceArray[row];
    cell.rowLabel.text = RX_STRING_FORMAT(@"%ld", (long)row);
    cell.titleLabel.text = model.title;
    cell.descLabel.text = model.desc;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self respondsToSelector:@selector(cellDidSelectRowAtIndexPath:)]) {
        [self cellDidSelectRowAtIndexPath:indexPath];
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end



#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincomplete-implementation"

@implementation BaseTableViewController (TableDidSelect)
- (void)addModelWidth:(NSString *)title desc:(NSString *)desc {
    CellModel * model = RXCREATE_ALLOC(CellModel);
    model.title = title;
    model.desc = desc;
    [self.sourceArray addObject:model];
}
@end
#pragma clang diagnostic pop
